package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.stadium.api.req.ReqChallenge;
import com.quanyan.stadium.api.req.ReqGameSchedule;
import com.quanyan.stadium.api.req.ReqRecordGameScheduleResult;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.service.gameRules.GameRoundService;
import com.quanyan.stadium.service.gameRules.GameScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;

@RestController("apiGameSchedule")
@RequestMapping("/api/stadium/gameSchedule")
public class GameScheduleController {

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Autowired
    private GameRoundService gameRoundService;

    @Autowired
    private GameScheduleService gameScheduleService;


    private final static Logger logger = LoggerFactory.getLogger(GameScheduleController.class);
    /**
     * 生成对战表详情
     *
     * */
    @RequestMapping(value="/generateCompList", method = RequestMethod.POST)
    public APIResponse<?> generateCompList(@RequestBody @Validated ReqChallenge reqChallenge, BindingResult result) throws Exception {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(gameRoundService.generateCompList(reqChallenge));
    }

    /**
     * 录入结果
     *
     * */
    @RequestMapping(value="/recordGameScheduleResult", method = RequestMethod.POST)
    public APIResponse<?> recordGameScheduleResult(@RequestBody ReqRecordGameScheduleResult req, BindingResult result, HttpServletRequest request)  {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameScheduleService.recordGameScheduleResult((Integer) request.getAttribute("uid"), req);
    }

    /**
     * 更新赛程的时间\地点
     *
     * */
    @RequestMapping(value="/updateGameScheduleTimeAndPlaceUnit", method = RequestMethod.POST)
    public APIResponse<?> updateGameScheduleTimeAndPlaceUnit(@RequestBody @Validated ReqGameSchedule req, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameScheduleService.updateGameScheduleTimeAndPlaceUnit((Integer) request.getAttribute("uid"), req);
    }

}
